//
//  UserInfoManager.m
//  dzm
//
//  Created by mac on 14-3-4.
//  Copyright (c) 2014年 wt. All rights reserved.
//

#import "UserInfoManager.h"
#import "AppConfig.h"
@implementation UserInfoManager

+ (UserInfoManager *)sharedManager
{
    static UserInfoManager *sharedManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedManagerInstance = [[self alloc] init];
    });
    return sharedManagerInstance;
}

-(void)postLoginSuccessNotificenter{
    [[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccess object:nil];
}

-(void)postLogoutSuccessNotificenter{
    [[NSNotificationCenter defaultCenter] postNotificationName:LogoutSuccess object:nil];
}

-(void)updateLoginStatus:(BOOL)loginStatus{
   UserInfoModel *userInfo = [[UserInfoManager sharedManager] getUserInfo];
    userInfo.loginStatus = loginStatus;
   [[UserInfoManager sharedManager] saveUserInfo:userInfo];
    
    if (loginStatus) {
        [self postLoginSuccessNotificenter];
    }else{
        [self postLogoutSuccessNotificenter];
    }
    
}

+(NSString *) getDocumentPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

-(void)saveUserInfo:(UserInfoModel *)model
{
    NSDictionary *dic = [model toDictionary];
    NSString *documentPath = [UserInfoManager getDocumentPath];
    NSString *path = [documentPath stringByAppendingPathComponent:@"userInfo.plist"];
    [dic writeToFile:path atomically:YES];
}

//获取用户信息
-(UserInfoModel *)getUserInfo
{
    NSString *documentPath = [UserInfoManager getDocumentPath];
    NSString *path = [documentPath stringByAppendingPathComponent:@"userInfo.plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    UserInfoModel *model = [[UserInfoModel alloc] initWithDictionary:dic];
    return model;
}

@end
